/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   PurchaseEventListenner.java for com.pengtu.service.custom    </p>
 * 
 * <p>最后修改 : $: 2011-12-30-上午9:33:03 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.service.custom;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.pengtu.Constants;
import com.pengtu.entity.app.MessageInfo;
import com.pengtu.entity.app.User;
import com.pengtu.model.PersonalModel;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.business.HomeManager;
import com.pengtu.utils.spring.SpringContextHolder;

/**
 * 
 * PurchaseEventListenner
 * 
 * 2011-12-30 上午9:33:03
 * 
 * @version 1.0.0
 * 
 */
public class PurchaseEventListenner implements EventListener {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private String params;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbpm.api.listener.EventListener#notify(org.jbpm.api.listener.
	 * EventListenerExecution)
	 */
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		// TODO Auto-generated method stub
		SystemManager systemManager = SpringContextHolder
				.getBean("systemManager");
		AccountManager accountManager = SpringContextHolder
				.getBean("accountManager");
		HomeManager homeManager = SpringContextHolder.getBean("homeManager");
		ExecutionImpl executionImpl = (ExecutionImpl) execution;
		String id = executionImpl.getId().split("\\.")[1];
		String key = (String) executionImpl.getVariable(Constants.START_TYPE);
//		String key = executionImpl.getId().split("\\.")[0];
		PersonalModel personalModel = homeManager.getObject(id, key);
		if (!com.pengtu.utils.StringUtils.hasChinese(params)) {
			String toUser = (String) execution.getVariable(params);
			MessageInfo messageInfo = new MessageInfo();
			messageInfo.setType("1");
			messageInfo.setReadFlag("0");
			messageInfo.setReceivedel("0");
			messageInfo.setSenddel("0");
			messageInfo.setSubject("您有新的待办任务，请尽快处理！");
			messageInfo.setGrade("2");
			messageInfo.setContent("待办任务信息如下(请尽快处理)：\n标题："
					+ personalModel.getTitle() + "\n类型："
					+ personalModel.getType() + "\n执行节点："
					+ executionImpl.getActivityName());
			messageInfo.setReceiveUser(accountManager.getNameByUserName(toUser)
					.getId());
			messageInfo.setSendDate(new Date());
			systemManager.saveMessageInfo(messageInfo);
		}

		if (com.pengtu.utils.StringUtils.hasChinese(params)) {
			List<User> u_list = accountManager.getUserByRoleId(params);
			for (User user1 : u_list) {
				MessageInfo messageInfo = new MessageInfo();
				messageInfo.setType("1");
				messageInfo.setReadFlag("0");
				messageInfo.setReceivedel("0");
				messageInfo.setSenddel("0");
				messageInfo.setSubject("您有新的待办任务，请尽快处理！");
				messageInfo.setGrade("2");
				messageInfo.setContent("待办任务信息如下(请尽快处理)：\n标题："
						+ personalModel.getTitle() + "\n类型："
						+ personalModel.getType() + "\n执行节点："
						+ executionImpl.getActivityName());
				messageInfo.setReceiveUser(user1.getId());
				messageInfo.setSendDate(new Date());
				systemManager.saveMessageInfo(messageInfo);
			}
		}
	}
}
